" See plugin/bufkill.vim
noremap Q :q<cr>
noremap S :w<cr>
"Write buffer (save)
nnoremap <C-s> :<C-u>write<CR>

noremap <silent> <expr> j (v:count == 0 ? 'gj' :'j')
noremap <silent> <expr> k (v:count == 0 ? 'gk' :'k')


" 保存文件修改历史
silent !mkdir -p ~/.config/nvim_person/tmp/backup
silent !mkdir -p ~/.config/nvim_person/tmp/undo
set backupdir=~/.config/nvim_person/tmp/backup,.
set directory=~/.config/nvim_person/tmp/backup,.
if has('persistent_undo')
	set undofile
	set undodir=~/.config/nvim_person/tmp/undo,.
endif

" set visualbell
" vim自动进入上次编辑的位置
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

" ===
" === Terminal Behaviors
" ===
let g:neoterm_autoscroll = 1
autocmd TermOpen term://* startinsert
tnoremap <C-N> <C-\><C-N>
tnoremap <C-O> <C-\><C-N><C-O>
let g:terminal_color_0  = '#000000'
let g:terminal_color_1  = '#FF5555'
let g:terminal_color_2  = '#50FA7B'
let g:terminal_color_3  = '#F1FA8C'
let g:terminal_color_4  = '#BD93F9'
let g:terminal_color_5  = '#FF79C6'
let g:terminal_color_6  = '#8BE9FD'
let g:terminal_color_7  = '#BFBFBF'
let g:terminal_color_8  = '#4D4D4D'
let g:terminal_color_9  = '#FF6E67'
let g:terminal_color_10 = '#5AF78E'
let g:terminal_color_11 = '#F4F99D'
let g:terminal_color_12 = '#CAA9FA'
let g:terminal_color_13 = '#FF92D0'
let g:terminal_color_14 = '#9AEDFE'
augroup TermHandling
  autocmd!
  " Turn off line numbers, listchars, auto enter insert mode and map esc to
  " exit insert mode
  autocmd TermOpen * setlocal listchars= nonumber norelativenumber
    \ | startinsert
  autocmd FileType fzf call LayoutTerm(0.6, 'horizontal')
augroup END

function! LayoutTerm(size, orientation) abort
  let timeout = 16.0
  let animation_total = 120.0
  let timer = {
    \ 'size': a:size,
    \ 'step': 1,
    \ 'steps': animation_total / timeout
  \}

  if a:orientation == 'horizontal'
    resize 1
    function! timer.f(timer)
      execute 'resize ' . string(&lines * self.size * (self.step / self.steps))
      let self.step += 1
    endfunction
  else
    vertical resize 1
    function! timer.f(timer)
      execute 'vertical resize ' . string(&columns * self.size * (self.step / self.steps))
      let self.step += 1
    endfunction
  endif
  call timer_start(float2nr(timeout), timer.f, {'repeat': float2nr(timer.steps)})
endfunction

" Open autoclosing terminal, with optional size and orientation
function! OpenTerm(cmd, ...) abort
  let orientation = get(a:, 2, 'horizontal')
  if orientation == 'horizontal'
    new | wincmd J
  else
    vnew | wincmd L
  endif
  call LayoutTerm(get(a:, 1, 0.5), orientation)
  call termopen(a:cmd, {'on_exit': {j,c,e -> execute('if c == 0 | close | endif')}})
endfunction



" ===
" === Window management
" ===
" Use <space> + new arrow keys for moving the cursor around windows
" 分屏窗口切换
noremap <LEADER>k <C-w>k
noremap <LEADER>j <C-w>j
noremap <LEADER>h <C-w>h
noremap <LEADER>l <C-w>l

" Disable the default s key
noremap s <nop>

" split the screens to up (horizontal), down (horizontal), left (vertical), right (vertical)
noremap sk :set nosplitbelow<CR>:split<CR>:set splitbelow<CR>
noremap sj :set splitbelow<CR>:split<CR>
noremap sh :set nosplitright<CR>:vsplit<CR>:set splitright<CR>
noremap sl :set splitright<CR>:vsplit<CR>
" Resize splits with arrow keys
noremap <up> :res +5<CR>
noremap <down> :res -5<CR>
noremap <left> :vertical resize-5<CR>
noremap <right> :vertical resize+5<CR>

" Place the two screens up and down
noremap sv <C-w>t<C-w>K
" Place the two screens side by side
noremap sp <C-w>t<C-w>H

" Rotate screens
noremap srk <C-w>b<C-w>K
noremap srh <C-w>b<C-w>H

" Press <SPACE> + q to close the window below the current window
noremap <LEADER>q <C-w>j:q<CR>

" ===
" === Tab management
" ===
" Create a new tab with tu
noremap tb :tabe<CR>
" Move around tabs with tn and ti
noremap th :-tabnext<CR>
noremap tl :+tabnext<CR>
" Move hte tabs with tmn and tmi
noremap tmh :-tabmove<CR>
noremap tml :+tabmove<CR>

" ===
" === Other useful stuff
" ===
" Opening a terminal window ,quit(<C-N>),move<C-o>,
noremap <LEADER>] :cd %:h<CR> :set splitbelow<CR>:sp<CR>:term<CR>

" 纵向分屏
noremap <LEADER>[ :cd %:h<CR> :vs term://$SHELL<CR>

" 新标签打开
noremap <LEADER>/ :tabe term://$SHELL<CR>

" Call figlet
noremap tx :r !figlet

" 标点自动补全
inoremap <leader>. <++>



" ===
" === Basic Mappings
" ===
" Set <LEADER> as <SPACE>, ; as :

noremap J 5j
noremap K 5k
noremap H 0
noremap L $



" make Y to copy till the end of the line
nnoremap Y y$

" Copy to system clipboard(打开注释即vim使用系统剪贴板，而不是自己的寄存器)
" set clipboard=unnamedplus
vnoremap Y "+y

" ===
" === Markdown Settings
" ===
" Snippets
source ~/.config/nvim/snippets/md-snippets.vim
" auto spell
autocmd BufRead,BufNewFile *.md setlocal spell

noremap mo <Esc>:MarkdownPreview<CR>
noremap mf <Esc>:MarkdownPreviewStop<CR>
noremap mt <Esc>:MarkdownPreviewToggle<CR>

" ===
" === markdown-previews
" ===
"let g:mkdp_auto_start = 0
"let g:mkdp_auto_close = 1
"let g:mkdp_refresh_slow = 0
"let g:mkdp_command_for_global = 0
"let g:mkdp_open_to_the_world = 0
"let g:mkdp_open_ip = ''
"let g:mkdp_browser = 'firefox'
"let g:mkdp_echo_preview_url = 0
"let g:mkdp_browserfunc = ''
"let g:mkdp_preview_options = {
"    \ 'mkit': {},
"    \ 'katex': {},
"    \ 'uml': {},
"    \ 'maid': {},
"    \ 'disable_sync_scroll': 0,
"    \ 'sync_scroll_type': 'middle',
"    \ 'hide_yaml_meta': 1,
"    \ 'sequence_diagrams': {}
"    \ }
"let g:mkdp_markdown_css = ''
"let g:mkdp_highlight_css = ''
"let g:mkdp_port = ''
"let g:mkdp_page_title = '「${name}」'

" ===
" === vim-table-mode
" ===
noremap <LEADER>tm :TableModeToggle<CR>
"let g:table_mode_disable_mappings = 1
let g:table_mode_cell_text_object_i_map = 'k<Bar>'



" ===
" === ranger
" ===
let g:NERDTreeHijackNetrw = 0 
let g:ranger_replace_netrw = 1 
nnoremap ge :Ranger<cr>


" ===
" === Ultisnips
" ===
let g:tex_flavor = "latex"
inoremap <c-n> <nop>
let g:UltiSnipsExpandTrigger="<c-e>"
let g:UltiSnipsJumpForwardTrigger="<c-e>"
let g:UltiSnipsJumpBackwardTrigger="<c-n>"
let g:UltiSnipsSnippetDirectories = [$HOME.'/.config/nvim/coc/ultisnips/', 'ultiSnips']
silent! au BufEnter,BufRead,BufNewFile * silent! unmap <c-r>


" ===
" === Coc
" ===
"nmap tt :CocCommand explorer<CR>
" coc-translator
nmap tt <Plug>(coc-translator-pv)
nmap tr <Plug>(coc-translator-p)

" ===
" === theme
" ===

set termguicolors     " enable true colors support
"let ayucolor="light"  " for light version of theme
"let ayucolor="mirage" " for mirage version of theme
" let ayucolor="dark"   " for dark version of theme
" colorscheme ayu

set t_Co=256

" let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
" let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"


" ===
" === like Emac
" ===

" See plugin/bufkill.vim
" use emacs keybind
nmap <C-x>k :BD<CR>
"Write buffer (save)
nnoremap <C-s> :<C-u>write<CR>
"yank to end
nnoremap Y y$
" Whitespace jump (see plugin/whitespace.vim)
nnoremap ]w :<C-u>WhitespaceNext<CR>
nnoremap [w :<C-u>WhitespacePrev<CR>
" (无效)Remove spaces at the end of lines
nnoremap <silent> <Space>cw :<C-u>silent! keeppatterns %substitute/\s\+$//e<CR>

" insert keymap like emacs
inoremap <C-w> <C-[>diwa

inoremap <C-h> <BS>
inoremap <C-d> <Del>
" 删除前面所有内容
inoremap <C-u> <C-G>u<C-U>
" 删除后面所有内容
inoremap <C-k>  <ESC>d$a

inoremap <C-b> <Left>
inoremap <C-f> <Right>
" inoremap <C-i> <Up>
" inoremap <C-n> <Down>
inoremap <C-a> <ESC>^i
inoremap <C-e> <ESC>$i
inoremap <expr><C-e> pumvisible() ? "\<C-e>" : "\<End>"
"insert a newline
inoremap <C-O> <Esc>o
imap <C-S> <esc>:w<CR>
imap <C-Q> <esc>:wq<CR>
" command line alias
cnoremap <C-p> <Up>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-d> <Del>
cnoremap <C-h> <BS>
cnoremap <C-t> <C-R>=expand("%:p:h") . "/" <CR>

" 未知
tnoremap <A-[> <C-\><C-n>


" Session management shortcuts (see plugin/sessions.vim)
nmap <Leader>ss :<C-u>SessionSave<CR>
nmap <Leader>sl :<C-u>SessionLoad<CR>

" ===
" === dashboard
" ===

"=== theme
"let g:dashboard_default_header = "doraemon"
let g:dashboard_default_header = "commicgirl2"
"let g:dashboard_default_header = "pikachu"

" ===
" ===Rust
" ===
let g:rustfmt_autosave = 1
